<template>
  <!-- 修改仓库 -->
  <div class="stock">
    <div class="dropdown">
      <jc-button
        :btn-arr="['reload', 'pass', 'save', 'sub', 'option', 'remove', 'list']"
        :disab-arr="[isDisabled && 'save', 'close', 'cancel', 'com1', 'com2', 'freeze']"
        :list="'/stockList'"
        @save="subMarker"
        @submit="subMarker(1)"
        @noSubmit="submit(2)"
        @audit="audit(1)"
        @noAudit="audit(2)"
        @startUsing="using(1)"
        @forbidden="using(2)"
        @del="delet()"
        @refresh="stockDetails"
      />
    </div>
    <el-tabs v-model="activeName" @tab-click="handleOther">
      <el-tab-pane :label="$t('market.info')" name="first">
        <el-form ref="purchaseRef" :model="prodValue" size="mini" label-width="100px">
          <el-form-item :label="$t('market.documentstatus')">
            <el-tag :type="prodValue.stateColor">{{ prodValue.fdocumentstatus }}</el-tag>
          </el-form-item>
          <el-form-item :label="$t('market.forbidStatu')">
            <el-tag v-if="prodValue.fforbidstatus === 'A'">{{ $t('market.normal') }}</el-tag>
            <el-tag v-else type="danger">{{ $t('btn.disab') }}<!--禁用--></el-tag>
          </el-form-item>
          <el-form ref="purchaseRef" :model="prodValue" label-width="100px">
            <el-form-item :label="$t('market.stockname')">
              <el-input v-model.trim="prodValue.fname" size="mini" :disabled="isDisabled" />
            </el-form-item>
            <el-form-item :label="$t('market.stockNumber')">
              <el-input v-model.trim="prodValue.fnumber" disabled size="mini" />
            </el-form-item>
            <el-form-item :label="$t('market.stockType')">
              <el-select v-model.trim="prodValue.ftype" size="mini" filterable :disabled="isDisabled">
                <el-option
                  v-for="(item, index) in billtypes"
                  :key="index"
                  :label="item.fname"
                  :value="item.fid"
                />
              </el-select>
            </el-form-item>
            <el-form-item :label="$t('market.stockgroup')">
              <el-input v-model.trim="prodValue.fstockgroup" size="mini" :disabled="isDisabled">
                <i
                  slot="suffix"
                  class="el-input__icon el-icon-search"
                  @click="stockVisiblit=true"
                />
              </el-input>
            </el-form-item>
            <el-form-item :label="$t('market.address')">
              <el-input v-model.trim="prodValue.faddress" size="mini" :disabled="isDisabled" />
            </el-form-item>
            <el-form-item :label="$t('market.stocker')">
              <el-autocomplete
                v-model.trim="prodValue.fstockerName"
                highlight-first-item
                popper-class="my-autocomplete"
                :fetch-suggestions="querySearch3"
                size="mini"
                :disabled="isDisabled"
                @focus="querySalesperson(3)"
                @select="marketData"
              >
                <i
                  slot="suffix"
                  class="el-input__icon el-icon-search"
                  @click="marketVisiblit=true"
                />
                <template slot-scope="{ item }">
                  <div class="name">{{ item.fuserName }}</div>
                </template>
              </el-autocomplete>
            </el-form-item>
            <el-form-item :label="$t('market.descrip')" style="width: 33.2%">
              <el-input v-model.trim="prodValue.fdescripTion" size="mini" :disabled="isDisabled" style="height: 35px" />
            </el-form-item>
          </el-form>
        </el-form>
      </el-tab-pane>
      <el-tab-pane :label="$t('market.other')" name="log">
        <jc-marker
          ref="log"
          :fnumber="prodValue.fnumber"
          other-height="68vh"
          :other-url-object="otherUrlObject"
        />
      </el-tab-pane>
    </el-tabs>
    <!--销售员列表-->
    <market v-if="marketVisiblit" :staff-type="staffType" @market="marketData" />
    <stockTree v-if="stockVisiblit" @stock="treeData" />
  </div>
</template>

<script>
import {
  updateStock, queryTBdStockDetails, submitStock,
  notsubmitStock, reviewStock, notreviewStock, ableStock, disableStock, deleteStock
} from '@/api/repertory/stock'
import { queryViewItem } from '@/api/marketManage/marketOrder'
import autocomplete from '@/views/market/marketManage/createMarkerOrder/components/autocomplete'
import { toMxBmina } from '@/components/ToMxamineState'
import jcMarker from '@/components/marker'
import jcButton from '@/components/Button'

export default {
  components: {
    stockTree: () => import('@/views/repertory/stock/components/stockTree'),
    market: () => import('@/views/market/marketManage/createMarkerOrder/components/market'),
    jcMarker,
    jcButton
  },
  mixins: [autocomplete],
  data() {
    return {
      isDisabled: false,
      staffType: 3, // 仓管员
      activeName: 'first',
      marketVisiblit: false,
      stockVisiblit: false,
      billtypes: [], // 类型
      prodValue: {
        faddress: '',
        fname: '',
        fnumber: '',
        ftype: 170,
        fstockerId: 0,
        fstockerName: '',
        fdescripTion: '',
        fdocumentstatus: ''
      },
      otherUrlObject: {}
    }
  },
  created() {
    this.queryTBasBilltype()
    this.stockDetails()
  },
  methods: {
    // 保存
    subMarker(ev) {
      const { faddress, fnumber, fname, ftype, fstockerId, fstockId, fdescripTion, fstockgroupid } = this.prodValue
      const DATA = {
        faddress, fnumber, fname, ftype, fstockerId, fstockId, fdescripTion, fstockgroupid
      }
      updateStock(DATA).then(res => {
        if (res.code === 0) {
          ev === 1 ? this.submit(1) : this.stockDetails()
          this.$message({ showClose: true, message: res.message, type: 'success' })
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            type: 'warning'
          })
        }
      })
    },
    // 详情
    stockDetails() {
      queryTBdStockDetails({ fstockId: this.$route.params.id }).then(res => {
        if (res.code === 0) {
          this.prodValue = toMxBmina(res.data)
          this.isDisabled = this.prodValue.fdocumentstatus === this.$t('market.state1') ||
            this.prodValue.fdocumentstatus === this.$t('market.state2')
          this.otherUrlObject = res.data
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            type: 'warning'
          })
        }
      })
    },
    // 查询单据类型
    async queryTBasBilltype() {
      const { data: RES } = await queryViewItem({ viewName: 'SX_STOCKTYPE' })
      this.billtypes = RES.list
    },
    // 获取销售员数据(子传父)
    marketData(item) {
      if (item.fuserId) {
        this.prodValue.fstockerName = item.fuserName
        this.prodValue.fstockerId = item.fuserId
      }
      this.marketVisiblit = false
    },
    // 仓库分组
    treeData(item) {
      if (item.fid) {
        this.prodValue.fstockgroup = item.fname
        this.prodValue.fstockgroupid = item.fid
      }
      this.stockVisiblit = false
    },
    // 公共函数
    toExamineFun(api) {
      const DATA = { Numbers: [this.prodValue.fnumber] }
      api(DATA).then(res => {
        if (res.code !== 0) {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
          return
        }
        this.stockDetails()
        this.innerHTM = ''
        res.data.forEach(res => {
          this.innerHTM += '<div>' + res + '</div>'
        })
        this.$notify.warning({
          title: '提示',
          message: this.innerHTM,
          duration: 3000, offset: 50,
          dangerouslyUseHTMLString: true
        })
      })
    },
    // 提交
    async submit(ev) {
      const api = ev === 1 ? submitStock : notsubmitStock
      this.toExamineFun(api)
    },
    // 审核
    async audit(ev) {
      const api = ev === 1 ? reviewStock : notreviewStock
      this.toExamineFun(api)
    },
    // 禁用启用
    async using(ev) {
      const api = ev === 1 ? ableStock : disableStock
      this.toExamineFun(api)
    },
    delet() {
      this.$confirm('确认要删除么？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        const DATA = { Numbers: [this.prodValue.fnumber] }
        deleteStock(DATA).then(res => {
          if (res.code !== 0) {
            this.$confirm(res.message, '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'error'
            })
            return
          }
          // 关闭当前页面
          this.$store.dispatch('tagsView/guanbi', this.$route)
          this.$router.push({ path: '/stockList' })
        })
      })
    },
    // 操作记录
    handleOther(tab) {
      if (tab.name === 'log') {
        this.$refs.log.otherlog()
      }
    }
  }
}
</script>

<style scoped lang="scss">
.stock {
  padding: 10px;
  background: #fff;
  min-height: calc(100vh - 100px);
  .el-input::v-deep .el-input__inner {
    height: 30px;
    line-height: 30px;
  }
  .el-form {
    display: flex;
    flex-direction: column;
    flex-wrap: wrap;

    .el-form-item {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      height: 35px;
      width: 20%;
      margin-bottom: 5px;
      .btnType {
        margin-right: 20px;
      }

      .el-date-editor.el-input {
        width: 100% !important;
      }

      .el-select {
        width: 100%;
      }
    }
  }
}
</style>
